package com.vancouver.knows.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vancouver.knows.business.bean.SearchResultInfo;

/**
 * 搜索总列表解析
 * Created by llf
 */
public class SearchTotalInfoHandle extends ACheckableJsonParser {

	public static final String SEARCH_MAP = "search_map";
	private HashMap<String, Object> resultMap = new HashMap<String, Object>();

	@Override
	protected void parserContent(JSONObject content) throws Exception {
		int sizePostResult = 0;
		int sizeEventResult = 0;
		int sizeBusinessResult = 0;
		
		if (!resultMap.isEmpty()){
			resultMap.clear();
			resultMap = null;
		}
		//====================搜索数据部分=============================
		List<SearchResultInfo> postResultInfoList = new ArrayList<SearchResultInfo>();
		try {
			if (content.getJSONObject("post_result") != null) {
				JSONObject jsonPostResult = content.getJSONObject("post_result");
				JSONArray array = jsonPostResult.getJSONArray("post_list");
				if (array == null || array.size() == 0){
					sizePostResult = 1;
					SearchResultInfo info = new SearchResultInfo();
					info.setId("1");
					
					info.setSize(sizePostResult);//默认size = 1；
					postResultInfoList.add(info);
				}else{
					sizePostResult = array.size();
					for(int i = 0; i < sizePostResult; i++){
						SearchResultInfo info = new SearchResultInfo();
						info.setId(array.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array.getJSONObject(i).getString("post_title"));

						postResultInfoList.add(info);
					}
				}
			}
		} catch (Exception e) {
		}
		try {
			if (content.getJSONObject("event_result") != null){
				JSONObject jsonEventResult = content.getJSONObject("event_result");
				JSONArray array = jsonEventResult.getJSONArray("post_list");
				if (array == null || array.size() == 0){
					sizeEventResult = 1;
					SearchResultInfo info = new SearchResultInfo();
					info.setId("1");
					info.setSize(sizeEventResult);//默认size = 1；
					postResultInfoList.add(info);
				}else{
					sizeEventResult = array.size();
					for(int i = 0; i < sizeEventResult; i++){
						SearchResultInfo info = new SearchResultInfo();
						info.setId(array.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array.getJSONObject(i).getString("post_title"));
	
						postResultInfoList.add(info);
					}
				}
			}
		}catch (Exception e) {
		}
			
		try {
			if (content.getJSONObject("business_result") != null){
				JSONObject jsonBusinessResult = content.getJSONObject("business_result");	
				JSONArray array = jsonBusinessResult.getJSONArray("post_list");
				if (array == null || array.size() == 0){
					sizeBusinessResult = 1;
					SearchResultInfo info = new SearchResultInfo();
					info.setId("1");
					info.setSize(sizeBusinessResult);//默认size = 1；
					postResultInfoList.add(info);
				}else{
					sizeBusinessResult = array.size();
					for(int i = 0; i < sizeBusinessResult; i++){
						SearchResultInfo info = new SearchResultInfo();
						info.setId(array.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array.getJSONObject(i).getString("post_title"));

						postResultInfoList.add(info);
					}
				}
			}
		} catch (Exception e) {
		}

		try {
			if (content.getJSONObject("userpost_result") != null){
				JSONObject jsonBusinessResult = content.getJSONObject("userpost_result");
				JSONArray array = jsonBusinessResult.getJSONArray("post_list");
				if (array == null || array.size() == 0){
					sizeBusinessResult = 1;
					SearchResultInfo info = new SearchResultInfo();
					info.setId("1");
					info.setSize(sizeBusinessResult);//默认size = 1；
					postResultInfoList.add(info);
				}else{
					sizeBusinessResult = array.size();
					for(int i = 0; i < sizeBusinessResult; i++){
						SearchResultInfo info = new SearchResultInfo();
						info.setId(array.getJSONObject(i).getString("post_id"));
						info.setPostTitle(array.getJSONObject(i).getString("post_title"));

						postResultInfoList.add(info);
					}
				}
			}
		} catch (Exception e) {
		}

		resultMap.put(SEARCH_MAP, postResultInfoList);
	}
	
	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}

}
